package com.leetcode.divide;

import java.util.PriorityQueue;

/**
 * @author Dennis Li
 * @date 2020/9/13 22:44
 */
public class KClosest_973 {

    public int[][] kClosest(int[][] points, int K) {
        int[][] res = new int[K][2];

        PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> {
            long p1 = o1[0] * o1[0] + o1[1] * o1[1];
            long p2 = o2[0] * o2[0] + o2[1] * o2[1];
            return Long.compare(p1, p2);
        });

        for (int[] pair : points) {
            queue.offer(pair);
        }

        for (int i = 0; i < K; i++) {
            res[i] = queue.poll();
        }

        return res;
    }

}
